/* 
 * Coded by David Bruno Cortarello <Nomius>. Redistribute under the terms of
 * the BSD-lite license. Bugs, suggests, nor projects: dcortarello@gmail.com.
 *
 * Program: Limshsql
 * Version: 2.0
 *
 *
 * Copyright (c) 2011, David B. Cortarello
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *   * Neither the name of Kwort nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <string.h>
#include <menu.h>
#include "limshsql.h"
#include "config.h"

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{
	int length, x, y;
	float temp;

	if (win == NULL)
		win = stdscr;
	getyx(win, y, x);
	if (startx != 0)
		x = startx;
	if (starty != 0)
		y = starty;
	if (width == 0)
		width = COLS;

	length = strlen(string);
	temp = (width - length) / 2;
	x = startx + (int)temp;
	wattron(win, color);
	mvwprintw(win, y, x, "%s", string);
	wattroff(win, color);
	refresh();
}


void ShowHelp(MENU *my_menu)
{
	int my_choice;
   
	my_choice= item_index(current_item(my_menu));

	move(LINES - 2, 1);
	clrtoeol();
	attron(COLOR_PAIR(2));
	mvprintw(LINES - 2, 1, "%s\n", MenuItems[my_choice].extended_help == NULL ?
			"No help available" :
			MenuItems[my_choice].extended_help);
	attroff(COLOR_PAIR(2));
	refresh();
	pos_menu_cursor(my_menu);
}


int runMenu(void)
{
	char about[100];
	ITEM **my_items;
	int c = -1;
	MENU *my_menu;
	WINDOW *my_menu_win, *small_win;
	int i;
	int my_choice = -1;

	/* Initialize curses */
	initscr();
	start_color();
	cbreak();
	noecho();
	keypad(stdscr, TRUE);
	init_pair(1, COLOR_RED, COLOR_BLACK);
	init_pair(2, COLOR_CYAN, COLOR_BLACK);


	/* Create items */
	my_items = (ITEM **) calloc(items, sizeof(ITEM *));
	for (i = 0; i < items; ++i)
		my_items[i] = new_item(MenuItems[i].desc, MenuItems[i].command);

	/* Crate menu */
	my_menu = new_menu((ITEM **) my_items);

	/* Create the window to be associated with the menu */
	my_menu_win = newwin(LINES - 6, COLS - 2,  1, 1);
	keypad(my_menu_win, TRUE);

	/* Set main window and sub window */
	set_menu_win(my_menu, my_menu_win);
	set_menu_sub(my_menu, derwin(my_menu_win, LINES - 12, COLS - 10, 3, 1));

	/* Set menu mark to the string " * " */
	set_menu_mark(my_menu, " * ");

	/* Print a border around the main window, the title and the keys legend*/
	box(my_menu_win, 0, 0);
	print_in_middle(my_menu_win, 1, 0, COLS, "Options in menu", COLOR_PAIR(1));
	mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
	mvwhline(my_menu_win, 2, 1, ACS_HLINE, COLS - 4);
	mvwaddch(my_menu_win, 2, COLS - 3, ACS_RTEE);
	attron(COLOR_PAIR(2));
	mvprintw(LINES - 4, 1, "F10 to quit  -  F1 to show information");
	attroff(COLOR_PAIR(2));
	refresh();

	/* Post the menu */
	post_menu(my_menu);
	wrefresh(my_menu_win);

	/* After set ourself in the 1st option, update the help command legend */
	ShowHelp(my_menu);

	/* Handle the keys in the "main" loop */
	while (c != 10) {

		/* Read the key */
		c = wgetch(my_menu_win);

		switch (c) {

			case KEY_DOWN:
				menu_driver(my_menu, REQ_DOWN_ITEM);
				ShowHelp(my_menu);
				break;

			case KEY_UP:
				menu_driver(my_menu, REQ_UP_ITEM);
				ShowHelp(my_menu);
				break;

			case KEY_NPAGE:
				menu_driver(my_menu, REQ_SCR_DPAGE);
				ShowHelp(my_menu);
				break;

			case KEY_PPAGE:
				menu_driver(my_menu, REQ_SCR_UPAGE);
				ShowHelp(my_menu);
				break;

			/* Intro */
			case 10:
				my_choice = item_index(current_item(my_menu));
				refresh(); 
				pos_menu_cursor(my_menu);
				break;

			/* 'a', 'A' or F1 will show my info :-) */
			case 'a':
			case 'A':
			case KEY_F(1):
				small_win = newwin(5, 48, 6, (COLS - 48)/2);
				wattrset(small_win, COLOR_PAIR(1) | WA_BOLD);
				box(small_win, 0, 0);
				sprintf(about, "%s - %s", PACKAGE_NAME, PACKAGE_VERSION);
				mvwaddstr(small_win, 1, 2, about);
				mvwaddstr(small_win, 2, 2, "Developed by:");
				sprintf(about, "David B. Cortarello (%s)", PACKAGE_BUGREPORT);
				mvwaddstr(small_win, 3, 3, about);
				touchwin(small_win);
				wrefresh(small_win);
				getch();
				touchwin(my_menu_win);
				wrefresh(my_menu_win);
				pos_menu_cursor(my_menu);
				break;

			/* 'q', 'Q' or F10 will exit the program */
			case 'q':
			case 'Q':
			case KEY_F(10):
				my_choice = -1;
				c = 10;
				break;
		}
		wrefresh(my_menu_win);
	}

	/* Unpost and free all the memory taken up */
	unpost_menu(my_menu);
	free_menu(my_menu);
	for (i = 0; i < items; ++i)
		free_item(my_items[i]);
	endwin();

	return my_choice;
}

